import Vue from 'vue'
import App from './App.vue'
import router from "./router"
import store from "./store"
import axios from 'axios'
import "./assets/css/global.css"
// 导入字体图标
import '@/assets/fonts/iconfont.css'
// import less from 'less'
import "./plugins/element.js"
import "element-ui/lib/theme-chalk/index.css"
import TreeTable from "vue-table-with-tree-grid"
// import { showLoading, hideLoading } from '@/plugins/loading.js';
// 导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
// 导入富文本编辑器样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// 添加进度条效果
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 简单配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false, trickle: false })

// 配置请求根路径
Vue.prototype.base_url = "http://127.0.0.1:8000"
axios.defaults.baseURL = "http://127.0.0.1:8000/api/v1"
axios.interceptors.request.use(config => {
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem("token");
  return config
})

/* 请求之后的操作 */
axios.interceptors.response.use((response) => {
  NProgress.done()
  if (response.status >= 250  && response.status <= 260) {
      window.sessionStorage.removeItem('token') // 删除已经失效或过期的token（不删除也可以，因为登录后覆盖）
      router.replace({path: '/Login'}).catch(err => {err})
  } else if (response.data.token) {
      window.sessionStorage.setItem('token', response.data.token)
  }
  return response
})

Vue.filter('dataFormat', function (originVal) {
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')

  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  // yyyy-mm-dd hh:mm:ss
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

Vue.config.productionTip = false
Vue.prototype.axios = axios

// Vue.use(VueAxios, axios)
// Vue.use(ElementUI)
// Vue.use(less)
Vue.component("tree-table", TreeTable)
// 全局注册富文本编辑器
Vue.use(VueQuillEditor)


new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
